package cn.fx.pet.controller;

import cn.fx.basic.exception.BusinessException;
import cn.fx.basic.util.JsonResult;
import cn.fx.basic.util.LoginContext;
import cn.fx.pet.domain.SearchMasterMsg;
import cn.fx.pet.dto.AcceptDto;
import cn.fx.pet.service.ISearchMasterMsgService;
import cn.fx.pet.query.SearchMasterMsgQuery;
import cn.fx.basic.util.PageList;
import cn.fx.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (SearchMasterMsg)表控制层
 *
 * @author makejava
 * @since 2022-08-09 11:16:58
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    /**
     * 注入对象
     */
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    /**
     * 分页查询
     * @return 查询结果
     */
    @PostMapping("/toHandle")
    @ApiOperation(value = "待处理查询接口")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
	try {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return searchMasterMsgService.toHandle(query,logininfo.getId());
	} catch (Exception e) {
		e.printStackTrace();
        return new PageList<>();
	    }
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @PostMapping("/user")
    @ApiOperation(value = "待处理查询接口")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.user(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @PostMapping("/processed")
    @ApiOperation(value = "分页查询接口")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query) {
        try {
            return searchMasterMsgService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询")
    public SearchMasterMsg findOne(@PathVariable("id") Long id) {
	    return searchMasterMsgService.loadById(id);
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping()
    @ApiOperation(value = "查询所有")
    public List<SearchMasterMsg> findAll() {
	    return searchMasterMsgService.loadAll();
    }

    /**
     * 新增or修改数据
     *
     * @param searchMasterMsg 实体
     * @return 新增or修改结果
     */
    @PutMapping
    @ApiOperation(value = "添加or修改")
    public JsonResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
		if(searchMasterMsg.getId() == null){
			//添加操作
			searchMasterMsgService.save(searchMasterMsg);
		} else {
			//修改操作
			searchMasterMsgService.update(searchMasterMsg);
		}
		return JsonResult.me();
	} catch (Exception e) {
		e.printStackTrace();
        return JsonResult.me().setMsg("添加or修改失败");
	    }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
		searchMasterMsgService.remove(id);
		return JsonResult.me();
	} catch (Exception e) {
		e.printStackTrace();
        return JsonResult.me().setMsg("网络异常,稍后重试");
	    }
    }

    /**
     * 批量删除
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
		searchMasterMsgService.patchDel(ids);
		return JsonResult.me();
	} catch (Exception e) {
		e.printStackTrace();
        return JsonResult.me().setMsg("网络异常,稍后重试");
	    }
    }

    /**
     * 无限极接口  树
     *
     * @return 获取是否成功
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "无限极接口")
    public List<SearchMasterMsg> deptTree() {
       return searchMasterMsgService.deptTree();
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @PostMapping("/publish")
    @ApiOperation(value = "寻主接口")
    public JsonResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request) {
        try {
            //获取当前登录人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);

            searchMasterMsgService.publish(smm,logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }
    }

    /**
     * 拒单
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping ("/reject/{id}")
    @ApiOperation(value = "拒单")
    public JsonResult reject(@PathVariable("id")Long id) {
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }
    }

    /**
     * 拒单
     *
     * @param
     * @return 删除是否成功
     */
    @PostMapping ("/accept")
    @ApiOperation(value = "接单")
    public JsonResult accept(@RequestBody AcceptDto dto ,HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);

            searchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }
    }

}

